import { Application } from 'egg';
import { Instance } from 'sequelize';

type status = 'success' | 'error' | 'waiting';

export interface ListsAttributes {
  status: status;
  url: string;
  error_message: string;
}

export interface ListsInstance extends Instance < ListsAttributes >, ListsAttributes {
  id?: number;
  createdAt: Date;
  updatedAt: Date;
}

export default (app: Application) => {

  const { STRING, TEXT, ENUM } = app.Sequelize;

  const Lists = app.model.define< ListsInstance, ListsAttributes >('Lists', {
    url: STRING(255),
    error_message: TEXT,
    status: ENUM(['success', 'error', 'waiting'])
  });

  Lists.associate = () => {
    Lists.belongsTo(app.model.Pages);

    Lists.hasOne(app.model.Details);
  };

  return Lists;
};